package database;

import Settings.ServerData;
import Settings.TestInfo;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashSet;
import org.apache.log4j.Logger;

public class LoadFromDatabase {

    private static Logger logger = Logger.getLogger(LoadFromDatabase.class);
    private static LoadFromDatabase loadFromDBobj = null;

    public LoadFromDatabase() {
    }

    public static LoadFromDatabase getInstance() {
        if (loadFromDBobj == null) {
            createLoadFromDBObj();
        }
        return loadFromDBobj;
    }

    private static synchronized void createLoadFromDBObj() {
        if (loadFromDBobj == null) {
            try {
                loadFromDBobj = new LoadFromDatabase();
            } catch (Exception ex) {
                logger.fatal("exception while creating LoadFromDB", ex);
            }

        }

    }

    public boolean loadServerSettings() {
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet res = null;
        try {
            con = MyConnection.getInstance().getConnection();
            try {
                stmt = con.prepareStatement("SELECT * FROM settings");
                res = stmt.executeQuery();

                while (res.next()) {
                    String key = res.getString("key");
                    String value = res.getString("value");
                    ServerData.settings.put(key, value);
                }

            } catch (Exception ex) {
                logger.fatal("Can not load Settings", ex);
                return false;
            }

        } catch (Exception ex) {
            //connection exception
            logger.fatal("Can not create database connection", ex);
            return false;
        } finally {
            MyConnection.close(con);
            MyConnection.close(stmt);
            MyConnection.close(res);
        }
        return true;
    }

    public boolean loadTestSettings() {
        Connection con = null;
        PreparedStatement stmt = null;
        ResultSet res = null;

        try {
            con = MyConnection.getInstance().getConnection();
            try {
                stmt = con.prepareStatement("SELECT * FROM test_settings WHERE `status` = \"active\"");
                res = stmt.executeQuery();
                
                ServerData.testSettingsObjects.clear();
                
                while (res.next()) {

                    TestInfo obj = new TestInfo();
                    obj.pk_id = res.getInt("pk_id");
                    obj.port = res.getInt("port");
                    obj.protocol_id = res.getInt("protocol_id");
                    obj.protocol_name = res.getString("protocol_name");
                    obj.status = res.getString("status");
                    obj.transmission_protocol = res.getString("transmission_protocol");
                    obj.fileContentClient = res.getString("file_content_client");
                    obj.fileContentServer = res.getString("file_content_server");
                    obj.basicAppStatus = res.getString("basic_app_status");
                    ServerData.testSettingsObjects.add(obj);
                 
                           
                }

            } catch (Exception ex2) {
                logger.fatal("Can not load basic test Settings", ex2);
                return false;
            }

        } catch (Exception ex3) {
            //connection exception
            logger.fatal("Can not create database connection", ex3);
            return false;
        } finally {
            MyConnection.close(con);
            MyConnection.close(stmt);
            MyConnection.close(res);
        }
        return true;
    }
}
